package com.young.shop.api.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.young.shop.api.dao.TbMessageMapper;
import com.young.shop.api.service.TbMessageService;
import com.young.shop.pojo.entity.TbMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-01-27
 */
@Service
public class TbMessageServiceImpl extends ServiceImpl<TbMessageMapper, TbMessage> implements TbMessageService {

    @Autowired
    private TbMessageMapper tbMessageMapper;


    /**
     * 查找 接收者 为 recipient 的消息
     * @param recipient
     * @return
     */
    @Override
    public List<TbMessage> findListTbMessageByRecipient(String recipient) {
        List<TbMessage> tbMessageByRecipientList = tbMessageMapper.selectListTbMessageByRecipient(recipient);
        return tbMessageByRecipientList;
    }


    /**
     * 查找 发送者 为 sender 的消息
     * @param sender
     * @return
     */
    @Override
    public List<TbMessage> findListTbMessageBySender(String sender) {
        List<TbMessage> tbMessageBySenderList = tbMessageMapper.selectListTbMessageBySender(sender);
        return tbMessageBySenderList;
    }


    /**
     * 发送者 接收者 消息内容 组成一条数据插入到数据库中
     * @param
     * @return
     */
    @Override
    public int addOneMessage(String sender,String recipient,String message) {
        TbMessage tbMessage = new TbMessage();
        tbMessage.setSender(sender);
        tbMessage.setRecipient(recipient);
        tbMessage.setMessage(message);
        int i = tbMessageMapper.insertOneMessage(tbMessage);
        return i;
    }


    /**
     * 发送者 接收者 消息内容 组成一条数据插入到数据库中 状态为 1 (已读)
     * @param sender
     * @param recipient
     * @param message
     * @return
     */
    @Override
    public int addOMessageReadStateIs1(String sender, String recipient, String message) {
        TbMessage tbMessage = new TbMessage();
        tbMessage.setSender(sender);
        tbMessage.setRecipient(recipient);
        tbMessage.setMessage(message);
        int i = tbMessageMapper.insertOneMessage(tbMessage);
        return i;
    }


    /**
     * 修改 对应id 消息 的状态 为 1(已阅读)
     * @param id
     * @return
     */
    @Override
    public int modifyReadStateTo1ById(Integer id) {
        int i = tbMessageMapper.updateReadStateTo1ById(id);
        return i;
    }
}
